/*@return the number of the days in the current month*/
function daysInMonth()
{ 
    var time = new Date();
    var Year = time.getFullYear();
	var Month = time.getMonth();
	return 32 - new Date(Year, Month, 32).getDate();
}

// Store the name/value pair as a cookie, encoding the value with
// encodeURIComponent() in order to escape semicolons, commas, and spaces.
// If daysToLive is a number, set the max-age attribute so that the cookie
// expires after the specified number of days. Pass 0 to delete a cookie.
function setCookie(name, value, daysToLive) 
{
    var cookie = name + "=" + encodeURIComponent(value);
    if (typeof daysToLive === "number")
        cookie += "; max-age=" + (daysToLive*60*60*24);
    document.cookie = cookie;
}

// Return the document's cookies as an object of name/value pairs.
// Assume that cookie values are encoded with encodeURIComponent().
function getCookies() 
{
    var cookies = {};
    // The object we will return
    var all = document.cookie; // Get all cookies in one big string
    if(all === "")
        // If the property is the empty string
        return cookies;
    // return an empty object
    var list = all.split("; "); // Split into individual name=value pairs
    for(var i = 0; i < list.length; i++) { // For each cookie
        var cookie = list[i];
        var p = cookie.indexOf("=");
        // Find the first = sign
        var name = cookie.substring(0,p);
        // Get cookie name
        var value = cookie.substring(p+1); // Get cookie value
        value = decodeURIComponent(value); // Decode the value
        cookies[name] = value;
        // Store name and value in object
    }
    return cookies;
}

function getKeys(obj){
   var keys = [];
   for(var key in obj){
      keys.push(key);
   }
   return keys;
}

function defaultRings()
{
    var data='month|'+ '250|'+ '#E0FFFF|'+ '#2B60DE|'+ '#2B60DE|' +'12|'+'15px brush script mt$'
	        +'date|'+ '208.35|'+ '#E6A9EC|'+ '#2B60DE|'+ '#2B60DE|'+'30|'+'15px algerian$'
			+'hrs|'+ '166.68|'+ '#57E964|'+ '#2B60DE|'+ '#2B60DE|'+'24|'+'11px Wide Latin$'
			+'mins|'+ '125|'+ '#ADA96E|'+ '#2B60DE|'+ '#2B60DE|'+'60|'+'10px deco$'
			+'secs|'+ '90|'+ '#C48793|'+ '#2B60DE|'+ '#2B60DE|'+'60|'+'8px deco$'
			+'week|'+ '60|'+ '#FFE87C|'+ '#2B60DE|'+ '#2B60DE|'+'7|'+'10px ravie$';
    setCookie("rings", data, 3600);	

}

/*
@param total total number of ring
@param position position of the ring within the clock 
@param name name of the ring
@param color color of the ring
@return function with setting for the ring
*/
function generateRing(total, position, name, color)
{
    var core=60+(6-total)*10;
	var size=250-((250-core)/(total-1))*position;
	if(position==total-1 && total>1)
	{
	    size=core;
	}
	var ringName;
	var font;
	var division;
	if(name=="monthshow")
	{
	    ringName="month";
		division=12;
		font="15px brush script mt"
	}
	else if(name=="dateshow")
	{
	    ringName="date";
		division=30;
		if(total>3 && position>3)
		{
		    font="11px algerian";
		}
		else
		{
		    font="15px algerian";
		}
	}
	else if(name=="hourshow")
	{
	    ringName="hrs";
		division=24;
		if(total>3 && position>3)
		{
		    font="8px Wide Latin";
		}
		else
		{
		    font="11px Wide Latin";
		}
	}
	else if(name=="minuteshow")
	{
	    ringName="mins";
		division=60;
		if(total>3 && position>3)
		{
		    font="10px deco";
		}
		else
		{
		    font="12px deco";
		}
	}
	else if(name=="secondshow")
	{
	    ringName="secs";
		division=60;
		if(total>3 && position>3)
		{
		    font="8px lighter deco";
		}
		else
		{
		    font="12px lighter deco";
		}
	}
	else if(name=="weekshow")
	{
	    ringName="week";
		division=7;
		if(total>3 && position>3)
		{
		    font="10px ravie";
		}
		else
		{
		    font="12px ravie";
		}
	}
	
	var data= ringName+'|'+ size+'|'+ color+'|'+ '#2B60DE|'+ '#2B60DE|' + division +'|'+ font +'$';
	return data;
}

//to calulate moon phases
function moonPhase()
{
    today = new Date();         // initialize to current date
    hh = today.getHours();          // goes from 0 to 23
    var ampm = "am"
        var NoLoops = hh        // this variable is also used for the cuckoo clock
            if(hh == 0) { NoLoops = 12};
            if(hh > 12) { NoLoops = hh - 12; ampm = "pm" }
            if(hh == 12) { ampm = "pm" }
    var dd = today.getDate();
    var mm = today.getMonth() + 1;  // Jan is 0, Feb is 1, etc., hence the +1
    var dow = today.getDay();
    MonNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    var ThisMonth = MonNames[mm];

    // var yy = today.getYear();
    // browser Y2K bug fix -- convert from msec to years because getYear() doesn't work right
    var millisec = today.getTime();            // this gives msecs
    var yy = ((((millisec / 1000) / 3600) / 24) / 365.25);
    yy = Math.floor(yy);
    yy +=1970;

    var txtDate = "" + ((dd < 10) ? "0" : "") + dd;   // add 0 if less than 10 so displays right
    txtDate += "&nbsp;" + ThisMonth;
    txtDate += "&nbsp;" + yy;
    var mn = today.getMinutes();        // goes from 0 to 59
    var txtTime = "" + ((NoLoops < 10) ? "0" : "") + NoLoops;
    txtTime += ((mn<10) ? ":0" : ":") + mn;

    // here's where the calculations from the book start
    var moondate = today;
    tzone = moondate.getTimezoneOffset() / 60               // in min so convert to hours
    var moonmsec = moondate.getTime();                      // this gives msecs
    GMtime = moonmsec + (tzone * 60 * 60 * 1000)            // GMT in msec
    // adapted from my VB code
    var startDate = new Date(89, 11, 31, 00, 00, 00);       // equivalent of the book's 0 Jan 90
    var startMsec = startDate.getTime();
    var dmsec = GMtime - startMsec;
    D = ((((dmsec /1000) /60) /60) /24);
    var n = D * (360 / 365.242191);                         //no 46-3
    if (n > 0) {
        n = n - Math.floor(Math.abs(n / 360)) * 360;    //no 46-3
    } else {
        n = n + (360 + Math.floor(Math.abs(n / 360)) * 360);  //no 46-3
    }
    var Mo = n + 279.403303 - 282.768422;                   //no 46-4;
    if(Mo < 0) { Mo = Mo + 360 }                         //no 46-4
    var Ec = 360 * .016713 * Math.sin(Mo * 3.141592654 / 180) / 3.141592654;        //no 46-5
    var lamda = n + Ec + 279.403303;                        //no 46-6
    if(lamda > 360) { lamda = lamda - 360 }              //no 46-6
    var l = 13.1763966 * D + 318.351648;                    //no 65-4
    if (l > 0) {
        l = l - Math.floor(Math.abs(l / 360)) * 360;    //no 65-4
    } else {
        l = l + (360 + Math.floor(Math.abs(l / 360)) * 360);  //no 65-4
    }
    var Mm = l - .1114041 * D - 36.34041;                   //no 65-5
    if (Mm > 0) {
        Mm = Mm - Math.floor(Math.abs(Mm / 360)) * 360; //no 65-5
    } else {
        Mm = Mm + (360 + Math.floor(Math.abs(Mm / 360)) * 360); //no 65-5
    }
    var N65 = 318.510107 - .0529539 * D;                    //no 65-6
    if (N65 > 0) {
        N65 = N65 - Math.floor(Math.abs(N65 / 360)) * 360;      //no 65-6
    } else {
        N65 = N65 + (360 + Math.floor(Math.abs(N65 / 360)) * 360);      //no 65-6
    }
    var Ev = 1.2739 * Math.sin((2 * (l - lamda) - Mm) * 3.141592654 / 180); //no 65-7
    var Ae = .1858 * Math.sin(Mo * 3.141592654 / 180);      //no 65-8
    var A3 = .37 * Math.sin(Mo * 3.141592654 / 180);        //no 65-8
    var Mmp = Mm + Ev - Ae - A3;                            //no 65-9
    var Ec = 6.2886 * Math.sin(Mmp * 3.141592654 / 180);    //no 65-10
    var A4 = .214 * Math.sin((2 * Mmp) * 3.141592654 / 180);        //no 65-11
    var lp = l + Ev + Ec - Ae + A4;                         //no 65-12
    var V = .6583 * Math.sin((2 * (lp - lamda)) * 3.141592654 / 180);       //no 65-13
    var lpp = lp + V;                                       //no 65-14
    var D67 = lpp - lamda;                                  //no 67-2
    Ff = .5 * (1 - Math.cos(D67 * 3.141592654 / 180));      //no 67-3
        Xx = (Math.sin(D67 * 3.141592654 / 180));
	
    if(Ff < .02) {
        //new moon
		var moon=0;
		return moon;
    }
    else if((Ff > .45) && (Ff < .55) && (Xx > 0)) {
        //first quarter
		var moon=2;
		return moon;
    }
    else if((Ff > .45) && (Ff < .55) && (Xx < 0)) {
        //last quarter
		var moon=6;
		return moon;
    }                
    else if(Ff > .98) {
        //full 
		var moon=4;
		return moon;
    }                                                                             
    else if((Ff > .02) && (Ff < .45) && (Xx > 0)) {
        //waxing crescent
		var moon=1;
		return moon;
    }
    else if((Ff > .02) && (Ff < .45) && (Xx < 0)) {
        //waning crescent
		var moon=7;
		return moon;
    }
    else if((Ff > .55) && (Ff < .98) && (Xx > 0)) {
        //waxing gibbous
		var moon=3;
		return moon;
    }
    else if((Ff > .55) && (Ff < .98) && (Xx < 0)) {
        //waning gibbous
		var moon=5;
		return moon;
    }
	var moon=-1;
	return moon;
}